﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMS.DAL.DAHelpers;
using SMS.Entities;
using System.Data;

namespace SMS.DAL
{
    public class AppHealthDA : BaseDA<UserActivity>
    {

        public override UserActivity SaveObject(string userName, UserActivity tObject)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteObject(string userName, UserActivity tObject)
        {
            throw new NotImplementedException();
        }

        public override List<UserActivity> GetObjectList(string userName, DateTime fromDate, DateTime toDate)
        {
            #region Procedure
            /**CREATE PROCEDURE USP_SELECT_USER_ACTIVITY
(@From_Date datetime, @To_Date datetime)
as
BEGIN
SELECT	ID,
		UserName,
		LogTime,
		Activity,
		ObjectString
		FROM UserActivity
		WHERE
		LogTime>=@From_Date and LogTime<=@To_Date
             */
            #endregion
            try
            {
                dbHelper.AddParameter("@From_Date", fromDate);
                dbHelper.AddParameter("@To_Date", toDate);
                var reader = dbHelper.ExecuteReader("USP_SELECT_USER_ACTIVITY", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                var list = new List<UserActivity>();
                while (reader.Read())
                {
                    list.Add(GetUserActivity(reader));
                }
                reader.Close();
                LogActivity(userName, string.Format("Get UserActivity from {0} to {1}", fromDate, toDate), null);
                return list;
            }
            catch (Exception ex)
            {
                LogException(userName, ex, null);
                throw new Exception("Error while getting User Activity");
            }

        }

        public List<ExceptionLog> GetExceptionHistory(string userName, DateTime fromDate, DateTime toDate)
        {
            #region Procedure
            /**
             * CREATE PROCEDURE USP_SELECT_EXCEPTION_HISTORY
(@From_Date datetime,@To_Date datetime)
as 
BEGIN
SELECT 
		ID,
		UserName,
		Exception_Time,
		Error,
		ErrorDescription
		 FROM ExceptionLog
		 WHERE Exception_Time> @From_Date AND Exception_Time<=@To_Date
END
             */
            #endregion
            try
            {
                dbHelper.AddParameter("@From_Date", fromDate);
                dbHelper.AddParameter("@To_Date", toDate);
                var reader = dbHelper.ExecuteReader("USP_SELECT_EXCEPTION_HISTORY", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                var list = new List<ExceptionLog>();
                while (reader.Read())
                {
                    list.Add(GetExceptionLog(reader));
                }
                reader.Close();
                LogActivity(userName, string.Format("Get GetException from {0} to {1}", fromDate, toDate), null);
                return list;
            }
            catch (Exception ex)
            {
                LogException(userName, ex, null);
                throw new Exception("Error while getting Exception History");
            }
        }

        public List<LoginHistory> GetLoginHistory(string userName, DateTime fromDate, DateTime toDate)
        {
            #region Procedure
            /**
             * ALTER PROCEDURE USP_SELECT_LOGIN_HISTORY
(@From_Date datetime,@To_Date datetime)
as
BEGIN
SELECT	ID,
		UserName
		,LoginTime,
		LoggedOutTime 
		FROM	
			LoginHistory
		WHERE LoginTime>=	@From_Date and LoginTime<=@To_Date
END
             */
            try
            {
                dbHelper.AddParameter("@From_Date", fromDate);
                dbHelper.AddParameter("@To_Date", toDate);
                var reader = dbHelper.ExecuteReader("USP_SELECT_LOGIN_HISTORY", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                var list = new List<LoginHistory>();
                while (reader.Read())
                {
                    list.Add(GetLoginHistory(reader));
                }
                reader.Close();
                LogActivity(userName, string.Format("Get GetLoginHistory from {0} to {1}", fromDate, toDate), null);
                return list;
            }
            catch (Exception ex)
            {
                LogException(userName, ex, null);
                throw new Exception("Error while getting Login History");
            }
            #endregion
        }

        public override List<UserActivity> SearchObject(string userName, string SearchText)
        {
            throw new NotImplementedException();
        }

        private UserActivity GetUserActivity(IDataReader reader)
        {
            int i = 0;
            var userActivity = new UserActivity();
            if (!reader.IsDBNull(i))
                userActivity.ID = reader.GetInt32(i++);
            if (!reader.IsDBNull(i))
                userActivity.UserName = reader.GetString(i++);
            if (!reader.IsDBNull(i))
                userActivity.LogTime = reader.GetDateTime(i++);
            if (!reader.IsDBNull(i))
                userActivity.Activity = reader.GetString(i++);
            if (!reader.IsDBNull(i))
                userActivity.ObjectString = reader.GetString(i++);
            return userActivity;
        }

        private ExceptionLog GetExceptionLog(IDataReader reader)
        {
            int i = 0;
            var exceptionLog = new ExceptionLog();
            if (!reader.IsDBNull(i))
                exceptionLog.ID = reader.GetInt32(i++);
            if (!reader.IsDBNull(i))
                exceptionLog.UserName = reader.GetString(i++);
            if (!reader.IsDBNull(i))
                exceptionLog.LogTime = reader.GetDateTime(i++);
            if (!reader.IsDBNull(i))
                exceptionLog.Error = reader.GetString(i++);
            if (!reader.IsDBNull(i))
                exceptionLog.ErrorDescription = reader.GetString(i++);
            return exceptionLog;

        }

        private LoginHistory GetLoginHistory(IDataReader reader)
        {
            int i = 0;
            var loginHistory = new LoginHistory();
            if (!reader.IsDBNull(i))
                loginHistory.ID = reader.GetInt32(i++);
            if (!reader.IsDBNull(i))
                loginHistory.UserName = reader.GetString(i++);
            if (!reader.IsDBNull(i))
                loginHistory.LoginTime = reader.GetDateTime(i++);
            if (!reader.IsDBNull(i))
                loginHistory.LogoutTime = reader.GetDateTime(i++);
            return loginHistory;
        }
    }
}
